package com.tjh.gulimall.order.controller;

import com.tjh.gulimall.order.entity.OrderEntity;
import com.tjh.gulimall.order.entity.OrderReturnReasonEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * day113：RabbitTemplate测试类
 *      测试：
 *          1、浏览器访问：localhost:9000/sendMq / http://order.gulimall.com/sendMq
 *          2、查看控制台输出
 *          3、访问 http://127.0.0.1:15672/#/ 观察MQ中Queues的消息数量、状态变化
 * Create by koala on 2021-05-16
 */
@RestController
@Slf4j
public class RabbitController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    //发送消息：用于测试 com.tjh.gulimall.order.service.impl.OrderItemServiceImpl 类中的 @RabbitHandler
    @GetMapping("/sendMq")
    public String sendMq(@RequestParam(value = "num",defaultValue = "10") Integer num){
        for (int i = 0;i < num;i++){
            if (i%2 == 0) {
                OrderReturnReasonEntity reasonEntity = new OrderReturnReasonEntity();
                reasonEntity.setId(1L);
                reasonEntity.setCreateTime(new Date());
                reasonEntity.setName("天客" + i);
                rabbitTemplate.convertAndSend("hello-java-exchange","hello6.java",reasonEntity,new CorrelationData(UUID.randomUUID().toString()));
                log.info("消息" + i + "发送成功{}",reasonEntity);
            } else {
                OrderEntity entity = new OrderEntity();
                entity.setOrderSn(UUID.randomUUID().toString());
                rabbitTemplate.convertAndSend("hello-java-exchange","hello6.java",entity,new CorrelationData(UUID.randomUUID().toString()));
            }
        }
        return "ok";
    }
}